//
//  FavoriteViewController.m
//  HDYS
//
//  Created by qianfeng on 15-5-4.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "FavoriteViewController.h"
#import "Header.h"
#import "DBManager.h"
#import "ListCarTableViewCell.h"
#import "GoodsInfoViewController.h"

@interface FavoriteViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UILabel *label;

//收藏数据数组
@property (nonatomic, strong) NSMutableArray *collectArray;


@end

@implementation FavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.collectArray = [NSMutableArray arrayWithCapacity:0];
    
    [self createTableView];
    
    /**
     创建编辑按钮
     */
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(ediBtnClick:)];
    [rightItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem= rightItem;
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    _label.backgroundColor = [UIColor whiteColor];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.collectArray removeAllObjects];
    self.collectArray = nil;
    
    DBManager *manager = [DBManager sharedManager];
    self.collectArray = (NSMutableArray *)[manager allData];
    
    if (self.collectArray.count != 0) {
        self.label.hidden = YES;
        [self.tableView reloadData];
    } else {
        self.label.hidden = NO;
        [self.tableView reloadData];
    }
}


/**
 *  创建数据
 */
-(void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.collectArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"listCarCell";
    ListCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ListCarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90.0f;
}

/**
 选中cell 进入详情页
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //传参数
    
    GoodsInfoViewController *goodsVC = [[GoodsInfoViewController alloc] init];
    goodsVC.hidesBottomBarWhenPushed = YES;
    [self presentViewController:goodsVC animated:YES completion:nil];
}

/**
 *  点击编辑按钮,进行删除
 */
-(void)ediBtnClick:(UIBarButtonItem *)item{
    if ([item.title isEqualToString:@"编辑"]) {
        [self.tableView setEditing:YES animated:YES];
        item.title = @"完成";
    } else{
        [self.tableView setEditing:NO animated:YES];
        item.title = @"编辑";
    }
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    DBManager *manager = [DBManager sharedManager];
    [manager deleteDataWithModel:self.collectArray[indexPath.row]];
    
    [self.collectArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
